<script>
import {ElMessage} from "element-plus";

export default {
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  data() {
    return {
      menuList: [],
      iconsObj:{
        '125':'Plus',
        '103':'<Minus />',
        '101':'<CirclePlus />',
        '102':'<Search />',
        '145':'<Male />',
      },
      isCollapse: false,
      activePath: '',
    }
  },

  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.replace('/login')
      ElMessage.info('退出登录')
    },
    async getMenuList() {
      const {data: res} = await this.$http.get('menus');
      if (res.meta.status !== 200) return ElMessage.error(res.meta.msg);
      // console.log(res.data)
      this.menuList = res.data;
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath){
      this.activePath = activePath
      window.sessionStorage.setItem('activePath',activePath);
    }

  }

}
</script>

<template>
  <div class="common-layout">
    <el-container class="home-container">
      <el-header>
        <div class="header-Logo">
          <img src="../assets/OIP-C.jfif" alt="" height="50" style="border-radius: 20%;">
          <span>后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px' ">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
              :unique-opened="true"
              :router="true"
              :default-active="activePath"
              :collapse="isCollapse"
              active-text-color="#409BFF"
              :collapse-transition="false"
              text-color="#3337444">
            <!--            一级菜单-->
            <el-sub-menu :index="item.id.toString()" v-for="item in menuList" :key="item.id">
              <template #title>
                <el-icon>
                  <el-icon><Coin /></el-icon>
                </el-icon>
                <span>{{ item.authName }}</span>
              </template>
              <!--            二级菜单-->
              <el-menu-item :index="subItem.path.toString()" v-for="subItem in item.children"
                            @click="saveNavState(subItem.path.toString())">
                <template #title>
                  <el-icon>
                    <Menu />
                  </el-icon>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-sub-menu>

          </el-menu>
        </el-aside>

        <el-main>
          <router-view></router-view>
        </el-main>

      </el-container>


    </el-container>
  </div>

</template>

<style scoped>

.home-container {
  height: 100%;
}

.common-layout {
  height: 100%;
}

.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #f2f2f2;
  font-size: 24px;
}

.el-aside {
  background-color: #333744;
}
.el-menu{
  border-right: 0;
}

.el-main {
  background-color: #EAEDF1;
}

.header-Logo {
  display: flex;
  align-items: center;

}

.header-Logo span {
  margin-left: 15px;
}
.toggle-button{
  background-color: #4A5064;
  color: #f2f2f2;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
